<?php
# Demo for using the RASS PHP Client Library (based on the Zend GData libraries).
# Copyright (C) 2008 rambla.be
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require_once './RawsClient/Raws/Rass.php';
define('BASE', 'http://rass.cdn01.rambla.be/');

# Provide your own credentials here
define('USER', 'xxx');
define('PWD', 'xxx');
# Provide your own path to a testfile, that can be uploaded
define('LOCAL_FILE', '../test_resources/bla.mp4');
# Provide your own path to a local download location (for a file to be retrieved from RASS)
define('TGT_FILE', "/path_to_download_location");


# all REST methods (except HEAD) will raise an exception if they don't return an HTTP SUCCESS CODE (200,201,204)
try {
  # Create a Rass instance, passing it your login credentials and the base service uri
  $rass = new Rass(USER, PWD, BASE);
  
  # Creates a "Simpsons" directory below your root dir
  $rass->createDir("Simpsons");
  # The uri for accessing this new dir will be available in the "Location" header of the response
  echo 'URI for Simpsons: ' . $rass->getResponse()->getHeader("Location") . "\n";

  # Creates a "movies" sub-directory below "Simpsons"
  $entry = $rass->createDir("Simpsons/Movies");
  # Retrieve the entry element's "kind" and "path" attributes
  echo "Created New " . $entry->kind . " entry with path = " . $entry->path . "\n";
  # The uri for accessing this new dir will be available in the "Location" header of the response
  echo "Location Header contains URI for Simpsons/Movies: " . $rass->getResponse()->getHeader("Location") . "\n";
  
  # Uploads "apu.mp4" to "Movies"
  $entry = $rass->createItem("Simpsons/Movies/apu.mp4", LOCAL_FILE);
  # Retrieve "kind" and "path" attributes
  echo "Created New " . $entry->kind . " entry with path = " . $entry->path . "\n";
  # The uri for accessing this new item will be available in the "Location" header of the response
  echo 'Location Header contains URI for apu.mp4: ' . $rass->getResponse()->getHeader("Location") . "\n";
  
  # Check if the file exists, by sending a HEAD request to the URI
  if ($rass->getItemHeader($rass->getResponse()->getHeader("Location"))) {
    echo 'apu.mp4 found: mimetype = ' . $rass->getResponse()->getHeader("Content-type") . ", filesize = " . $rass->getResponse()->getHeader("Content-length") . "\n";
  }
  # Download the file from the RASS server and store it in TGT_FILE
  $response = $rass->getItem($entry->path, TGT_FILE);
  if (200 == $response->getStatus()) {
    echo "File downloaded to location " . TGT_FILE . "\n";
  }
  else {
    echo "Failed downloading file to location " . TGT_FILE . ". Http status code = " . $get_item_status . "\n";
  }
  
  
  # Notice that getItemHeader doesn't raise errors in case the file doesn't exist, but null.
  if (! $rass->getItemHeader("Simpsons/apu.mp4")) {
    echo "No item apu.mp4 is present in the Simpsons dir.\n";
  }
  
  # Get a feed of evth beneath "/Simpsons"
  $feed = $rass->getDirFeed("Simpsons");
  foreach ($feed as $entry) {
    # Retrieve the entry element's "kind" and "path" attributes
    echo "Found " . $entry->kind . " entry with path = " . $entry->path . "\n";
    # Retrieve the value of the params child elements
    echo "Name: " . $entry->content->params->name->text . "\n";
    echo "Size: " . $entry->content->params->size->text . "\n";
    echo "Updated: " . $entry->content->params->updated->text . "\n";
  }
  
  # Pass a Query-String to limit the feed elements to files only
  $query = new Rass_DirQuery("file");
  $query->setKind("file");
  # Get a feed of evth beneath "Movies", passing the Query-obj as a second argument => only the 'file' entries will be returned (not 'root' or 'dir')
  $feed = $rass->getDirFeed("Simpsons/Movies", $query);
  foreach ($feed as $entry) {
    # Retrieve "kind" and "path" attributes
    echo "Found " . $entry->kind . " entry with path = " . $entry->path . "\n";
    # Retrieve params for this entry
    echo "Name: " . $entry->content->params->name->text . "\n";
    echo "Size: " . $entry->content->params->size->text . "\n";
    echo "Updated: " . $entry->content->params->updated->text . "\n";
  }

  # delete the file first. A directory can only be deleted when empty.
  $rass->deleteItem("Simpsons/Movies/apu.mp4");
  echo "apu.mp4 has been deleted.\n";
  # delete the sub-dir first. A directory can only be deleted when empty.
  $rass->deleteDir("Simpsons/Movies");
  echo "Movies has been deleted.\n";
  $rass->deleteDir("Simpsons");
  echo "Simpsons has been deleted.\n";

  # deleting dir that doesn't exist anymore => SHOULD RAISE EXCEPTION !!!!
  $rass->deleteDir("Simpsons");

}
catch(Zend_Gdata_App_Exception $e) {
    # Report the exception to the user
    echo "\nCaught exception: " . get_class($e) . "\n";
    echo "Message: " . $e->getMessage() . "\n";
    # get the HTTP status code
    echo "HTTP Status Code: " . $e->getResponse()->getStatus() . "\n";
    echo "Response Body with exception details: " . $e->getResponse()->getBody() . "\n";
    # get the raws:error elements
    $rawsResponse = Rass::parseRawsResponse($e->getResponse());
    echo "Raws Code: " . $rawsResponse->getCode() . "\n";
    echo "Raws Message: " . $rawsResponse->getMsg() . "\n";
    $reasons = $rawsResponse->getReasons();
    foreach ($reasons as $reason) {
      echo "Raws Reason: " . $reason . "\n";
    }
    $details = $rawsResponse->getDetails();
    foreach ($details as $key => $value) {
      echo "Raws Detail: " . $key . " -> " . $value . "\n";
    }
}
catch (Zend_Exception $e) {
    echo "Caught exception: " . get_class($e) . "\n";
    echo "Message: " . $e->getMessage() . "\n";
}
